package com.digital.welcome.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.digital.welcome.entity.College;
import com.digital.welcome.entity.User;
import com.digital.welcome.entity.UserCollege;
import com.digital.welcome.mapper.CollegeMapper;
import com.digital.welcome.mapper.UserCollegeMapper;
import com.digital.welcome.mapper.UserMapper;
import com.digital.welcome.response.Result;
import com.digital.welcome.service.UserCollegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class UserCollegeServiceImpl extends ServiceImpl<UserCollegeMapper, UserCollege> implements UserCollegeService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CollegeMapper collegeMapper;

    @Override
    public String getNumber(College college) {
        List<College> collegeList = collegeMapper.selectList(null);
        for (College c : collegeList) {
            if (c.getCollege().equals(college.getCollege())) {
                Integer count = null;
                QueryWrapper<User> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("college", c.getCollege());
                count = userMapper.selectCount(queryWrapper);
                return JSON.toJSONString(new Result<>().setCode(1).setMessage("查询成功").setData(count));
            }
        }
        return JSON.toJSONString(new Result<>().setCode(0).setMessage("不存在该学院"));
    }
}
